package com.ysc.plugin

import android.app.Activity
import android.content.Intent
import android.content.IntentFilter
import android.widget.Toast
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.ysc.plugin.callback.PluginCallBack
import com.ysc.plugin.common.LocalBroadcastAction
import com.ysc.plugin.common.abstract.Plugin
import com.ysc.plugin.utility.Utility
import com.ysc.plugin.utility.receiver.UtilityReceiver
import org.json.JSONObject


class PluginWrapper() {
    private var mActivity:Activity? = null
    private var mPluginMap:MutableMap<String,Plugin> = mutableMapOf()
    private var mCallBack = PluginCallBack()

    init {
        instance = this
    }

    fun executeCommand(data:String):String
    {
        if (data == null || data.isEmpty())
        {
            Toast.makeText(mActivity,"命令为空或者不存在", Toast.LENGTH_LONG).show()
            return ""
        }
        var decodeData = JSONObject(data)
        var command = ""
        var category = ""
        var operate = ""
        var parameter = ""
        if(decodeData.has("command"))
            command = decodeData["command"] as String
        if(decodeData.has("category"))
            category = decodeData["category"] as String
        if(decodeData.has("operate"))
            operate = decodeData["operate"] as String
        if(decodeData.has("parameter"))
            parameter = decodeData["parameter"] as String
        var intent = Intent()
        intent.setAction(LocalBroadcastAction.ACTION_UTILITY.name)
        intent.putExtra("command",command)
        intent.putExtra("category",category)
        intent.putExtra("operate",operate)
        if(!parameter.isEmpty())
            intent.putExtra("parameter",parameter)
        LocalBroadcastManager.getInstance(mActivity!!).sendBroadcastSync(intent)
        while(!mCallBack.checkExecuteFinished())
        mCallBack.setExecuteFinishedStatus(false)
        var callBackData = mCallBack.getCallBackData()
        mCallBack.setCallBackData("")
        return callBackData
    }

    fun initialize(activity:Activity){
        mActivity = activity
        var intentFilter = IntentFilter()
        intentFilter.addAction(LocalBroadcastAction.ACTION_UTILITY.name)
        LocalBroadcastManager.getInstance(mActivity!!).registerReceiver(UtilityReceiver(mCallBack),intentFilter)
        initPlugin()
    }

    fun getActivity():Activity{
        return mActivity!!
    }

    fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray)
    {
        for (plugin in mPluginMap){
            plugin.value.onRequestPermissionsResult(requestCode, permissions, grantResults)
        }
    }

    fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?){
        for (plugin in mPluginMap){
            plugin.value.onActivityResult(requestCode, resultCode, data)
        }
    }

    fun getPluginByName(name:String):Plugin?{
        return mPluginMap.get(name)
    }

    fun getPluginCounts():Int{
        return mPluginMap.size
    }

    private fun initPlugin(){
        addPlugin(Utility())
    }

    private fun addPlugin(plugin: Plugin) {
        plugin.initialize(mActivity!!)
        mPluginMap.put(plugin.getName(),plugin)
    }

    fun onDestroy() {
        LocalBroadcastManager.getInstance(mActivity!!).unregisterReceiver(UtilityReceiver(mCallBack))
        for (plugin in mPluginMap){
            plugin.value.onDestroy()
        }
    }

    companion object {
        private var instance:PluginWrapper?= null
        val ACTION_UTILITY :String = "com.ysc.broadcast.utility"
        fun getInstance(): PluginWrapper? {
            return instance
        }
    }
}